using System;
using System.Collections;
using System.Text;
using OpenCVForUnity.CoreModule;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using OpenCVRange = OpenCVForUnity.CoreModule.Range;
using Rect = UnityEngine.Rect;


public class MeasureScene : MonoBehaviour
{
    public AppViewCtrl appViewCtrl => GetComponentInParent<AppViewCtrl>();

    public Image frontImg;
    public Image sideImg;
    public GameObject showResultBtn;
    public BodyMeasureController bodyMeasureController;
    public RecordController recordController;

    public InputField realTallInput;
    public InputField realWeightInput;
    
    public Texture2D frontTex;
    public Texture2D sideTex;

    public GameObject errorMsg;
    
    private WebCamTexture webcamTexture;

    public bool test;

    private void Awake()
    {
        if(bodyMeasureController == null) bodyMeasureController = GetComponentInChildren<BodyMeasureController>();
        if (bodyMeasureController == null) bodyMeasureController = gameObject.AddComponent<BodyMeasureController>();
        if(recordController == null) recordController = GetComponentInChildren<RecordController>();
        
    }

    void Start()
    {
        errorMsg.gameObject.SetActive(false);
    }
    public void Begin()
    {
        bodyMeasureController.gameObject.SetActive(false);

        #if !UNITY_EDITOR
            test = false;
        #endif
        
        if (!test)
        {
            recordController.gameObject.SetActive(true);
            recordController.Begin(this.OnCapture);
        }
        else
        {
            DoActionDelay.DelayAction(1,(() =>
            {
                OnCapture(this.frontTex,this.sideTex);
            }));
        }
    }
    private void OnEnable()
    {
        // 获取可用的摄像头设备
        /*WebCamDevice[] devices = WebCamTexture.devices;

        if (devices.Length > 0)
        {
            // 使用第一个摄像头设备
            webcamTexture = new WebCamTexture(devices[0].name);
            webcamTexture.Play();
        }
        else
        {
            Debug.LogError("No camera device found.");
        }*/

    }
    
    void OnCapture(Texture2D frontTex,Texture2D sideTex)
    {
        this.frontTex = frontTex;
        this.sideTex = sideTex;
        Sprite sprite1 = Sprite.Create(frontTex, new Rect(0, 0, frontTex.width, frontTex.height), new Vector2(0.5f, 0.5f));
        Sprite sprite2 = Sprite.Create(sideTex, new Rect(0, 0, sideTex.width, sideTex.height), new Vector2(0.5f, 0.5f));
        this.frontImg.sprite = sprite1;
        this.sideImg.sprite = sprite2;
        
        bodyMeasureController.gameObject.SetActive(true);
        showResultBtn.gameObject.SetActive(true);
        recordController.gameObject.SetActive(false);
    }

    public void ShowErrorMsg()
    {
        errorMsg.gameObject.SetActive(true);
    }

    public void OnClickErrorMsgConfirm()
    {
        errorMsg.gameObject.SetActive(false);
    }

    public void OnClickGoBack()
    {
        recordController.OnGoback();
        appViewCtrl.ShowMeasureMainUI();
    }

    public void OnClickShowResult()
    {
        showResultBtn.gameObject.SetActive(false);
        DoActionDelay.DelayAction(.02f,DoCalResult);
    }
    void DoCalResult()
    {
        bool tallSuc = float.TryParse(this.realTallInput.text, out float tall);
        bool weightSuc = float.TryParse(this.realWeightInput.text, out float weight);
        if (!tallSuc || !weightSuc)
        {
            ShowErrorMsg();
            return;
        }
        
        bodyMeasureController.CalMeasureData(frontTex, sideTex,tall,weight, out FrontData frontData, out SideData sideData
        ,out Mat frontMat, out Mat sideMat);
        
        BodyReportData data = null;
        
        if (frontData != null && sideData != null)
        {
            data = bodyMeasureController.GetBodyReport(tall, weight, frontData, sideData);

            bodyMeasureController.AddResult2DebugView(frontMat, frontData,tall);
            bodyMeasureController.AddResult2DebugView(sideMat, sideData, tall);
        }

        Texture2D outputTex1 = PoseEstimationController.Mat2Tex(frontMat);
        Texture2D outputTex2 = PoseEstimationController.Mat2Tex(sideMat);
        
        Sprite sprite1 = Sprite.Create(outputTex1, new Rect(0, 0, outputTex1.width, outputTex1.height),
            new Vector2(0.5f, 0.5f));
        Sprite sprite2 = Sprite.Create(outputTex2, new Rect(0, 0, outputTex2.width, outputTex2.height),
            new Vector2(0.5f, 0.5f));

        if (frontData != null && sideData != null)
        {
            this.appViewCtrl.ShowBodyResultUI(data, sprite1,sprite2);
        }
        else
        {
            this.appViewCtrl.ShowErrorMag(sprite1,sprite2);
        }
    }

    [ContextMenu("TestPoseEstimation")]
    public void TestPoseEstimation()
    {
        bodyMeasureController.TestPoseEstimation(frontTex,sideTex);
    }

}
